package net.viralpatel.struts2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class DeleteUserAction extends ActionSupport{
	private String username;
	String url = "jdbc:mysql://localhost:3306/";
	String dbName = "t10";
	String driverName = "com.mysql.jdbc.Driver";
	Connection con = null;
	ResultSet resultSet = null;
	String nameroot = "root";
	String passwordroot = "";
	public String execute()throws Exception{
		if(deleteUser()==true){
			return SUCCESS;
		}else{
			addActionError(getText("error.deleteUser"));
			return ERROR;
		}
	}
	public boolean deleteUser()throws Exception{
		try {
			Class.forName(driverName);
			con = DriverManager.getConnection(url + dbName, nameroot,
					passwordroot);
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		String selectDeleteUser = "Delete FROM user Where userID=?";
		PreparedStatement prepStmt = con.prepareStatement(selectDeleteUser);
		prepStmt.setString(1, this.username);
		int row = prepStmt.executeUpdate();
		if(row==1){
			con.close();
			return true;
		}else
			con.close();
			return false;
	}
	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}
}
